/*****************************************************************************
 * file  : systask.c
 * creat : apleilx
 * data  : 2017/11/05
 *******************************************************************************/
#define _SYSTASK_MODULE_
#include "bsp.h"
#include "os_obj.h"
#include "sys_task.h"
#include "sys_init.h"
#include "mem_alloc.h"

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
void sys_tick_exe(void);

/*****************************************************************************
 * @brief   var.
 *****************************************************************************/

/*****************************************************************************
 * @brief   system task.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void task_system(const void *argument)
{
	uint32_t tick;

	sys_app_init();

	app_timer_task_add(sys_tick_exe);

	sys_st.buff = malloc(1000);
	dbg_print("malloc 1000: %8X\n", (uu32)(sys_st.buff));

	while (1)
	{
		sys_st.u_tick = app_tick_get();

		pulse_create(&sys_st.pulse, sys_st.u_tick);

		if (sys_st.pulse.pulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
		}

		sys_app_run_once();

		tick = app_tick_get();

		NOP1();
		osDelay(ms_ticks(10));
		NOP1();
	}
}

/*****************************************************************************
 * @brief   led control.
 *****************************************************************************/
void sys_led_ctr(void)
{
	static word_tt st = {1};

	if (!sys_st.pulse.pulse_10ms)
		return;

	// 位移
	if (sys_st.pulse.pulse_100ms)
	{
		// st.uval = bit16_roll_shift_left(st.uval, 4, 1);
	}

	// 输出
	// o_led1.out(st.bits.b0);
	// o_led2.out(st.bits.b1);
}
DBG_FUN_EXPORT(sys_led_ctr);

/*****************************************************************************
 *
 * @brief   task switch hook.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void task_sw_hook(char pos)
{
}
DBG_FUN_EXPORT(task_sw_hook);

/*****************************************************************************
 *
 * @brief   sys_par_load
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void sys_par_load(void)
{
}

/*****************************************************************************
 *
 * @brief   sys_tick_exe
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void sys_tick_exe(void)
{
}

/*****************************************************************************
 * @brief   system info.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void system_info(void)
{
	crm_clocks_freq_type clocks;

	crm_clocks_freq_get(&clocks);
	dbg_puts("===========================================================\n");
	dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
			  clocks.ahb_freq / 1000000,
			  clocks.apb1_freq / 1000000,
			  clocks.apb2_freq / 1000000);
	dbg_puts("===========================================================\n");
}

/*****************************************************************************
 * @brief   system info.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void mem_alloc_test(void)
{
	void *blk1s[20];
	void *blkns[8];
	
	blk_sram.init();
	mem_blk_info(&blk_sram);
	loop(20)
		blk1s[index] = blk_sram.malloc(120);

	blkns[0] = blk_sram.malloc(100);
	blkns[1] = blk_sram.malloc(120);
	blkns[2] = blk_sram.malloc(350);
	blkns[2] = blk_sram.malloc(350);
	blkns[2] = blk_sram.malloc(350);
	blkns[2] = blk_sram.malloc(350);
	blkns[3] = blk_sram.malloc(420);
	blkns[4] = blk_sram.malloc(800);
	blkns[5] = blk_sram.malloc(20);

	mem_blk_info(&blk_sram);

	blk_sram.free(blk1s[1]);
	blk_sram.free(blk1s[5]);
	blk_sram.free(blk1s[9]);

	blk_sram.free(blkns[1]);
	blk_sram.free(blkns[3]);
	blk_sram.free(blkns[3]);
	blk_sram.free(blkns[5]);

	blkns[5] = blk_sram.malloc(20);
	blk_sram.free(blkns[5]);

	dbg_puts("=====================loop 20 free=====================\n");
	loop(20)
		blk_sram.free(blk1s[index/2]);
		
	dbg_puts("=====================loop 10 alloc=====================\n");
	loop(10)
		blk1s[index] = blk_sram.malloc(120);
		
	dbg_puts("=====================loop 10 free=====================\n");
	loop(10)
		blk_sram.free(blk1s[index]);
		
	loop(8)
		blk_sram.free(blkns[index]);
		
	mem_blk_info(&blk_sram);
}
obj_init_order5(mem_alloc_test);

/*****************************************************************************
 * @brief   tick get.
 *
 * @ Pass/ Fail criteria: tick
 *****************************************************************************/
unsigned long rt_tick_get(void)
{
	return app_tick_get();
}

/*****************************************************************************
 * @brief   nsys hook
 *
 * @ Pass/ Fail criteria: tick
 *****************************************************************************/
